package com.wss.lsl.mongo.demo;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations;

import com.mongodb.gridfs.GridFSDBFile;

/**
 * Hello world!
 * 
 */
public class App7 {

	public static void main(String[] args) throws UnknownHostException {

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"SpringConfig2.xml");
		GridFsOperations operations = context.getBean("gridFsTemplate",
				GridFsOperations.class);

		List<GridFSDBFile> gridFSDBFiles = operations.find(new Query(Criteria
				.where("filename").is("我休闲.jpg")));

		int i = 1;
		for (GridFSDBFile fsdbFile : gridFSDBFiles) {
			System.out.println("filename: " + fsdbFile.getFilename()
					+ "\t contentType: " + fsdbFile.getContentType());
			try {
				fsdbFile.writeTo("D:\\Backup\\我的文档\\My Pictures\\我休闲" + i
						+ ".jpg");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			i++;
		}

		System.out.println("Done");

	}
}
